package com.xoa.controller.budget;

import com.alibaba.fastjson.JSONObject;
import com.xoa.model.budget.BudgetingProcessFlowRunPrcs;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.users.Users;
import com.xoa.service.WFE.WFERunner.WFERunner;
import com.xoa.service.budget.BudgetingProcessService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.common.wrapper.BaseWrappers;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/budget")
public class BudgetingProcessController {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    @Autowired
    BudgetingProcessService budgetingProcessService;
    @Autowired
    protected WFERunner wfeRunner;

    /**
     * 创建作者:   zxy
     * 创建日期:   2018-05-21
     * 方法介绍:   查询预算执行申请
     */
    @ResponseBody
    @RequestMapping(value = "/queryAllBudgetingProcess", produces = { "application/json;charset=UTF-8" })
    public ToJson<BudgetingProcessFlowRunPrcs> queryAllBudgetingProcess(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                                                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                                        @RequestParam(name = "useFlag", required = false, defaultValue = "true")  Boolean useFlag,
                                                                        String allow, HttpServletRequest request){
        return budgetingProcessService.queryAllBudgetingProcess(page,pageSize,useFlag,request,allow);
    }

/*
* 新建流程
* */
    @ResponseBody
    @RequestMapping(value = "/addBudgetingProcess", produces = { "application/json;charset=UTF-8" })
    public BaseWrapper addBudgetingProcess(String createDate, HttpServletRequest request){
        return budgetingProcessService.addBudgetingProcess(createDate,request);
    }
    /**
     * 创建作者:   zxy
     * 创建日期:   2018-05-24
     * 方法介绍:   执行流程审批
     */
    @ResponseBody
    @RequestMapping(value = "/approvalProcess", produces = { "application/json;charset=UTF-8" })
    public BaseWrappers approvalProcess(HttpServletRequest request, String num,
                                        @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                        @RequestParam(name = "useFlag", required = false, defaultValue = "true")  Boolean useFlag){
        return budgetingProcessService.approvalProcess(request,num,page,pageSize,useFlag);
    }

    /**
     * 作者: 张航宁
     * 日期: 2018/9/4
     * 说明: 文件管理查询
     * 参数： budgetItemName 项目名称 deptName 部门名称 applicant 申请人
     */
    @ResponseBody
    @RequestMapping("/selBudgetAttachs")
    public BaseWrappers selBudgetAttach(HttpServletRequest request, PageParams pageParams, String budgetItemName, String deptId, String applicant, Date startDate, Date endDate){
        return budgetingProcessService.selBudgetAttach(request,pageParams,budgetItemName,deptId,applicant,startDate,endDate);
    }

    /**
     * 作者: 张航宁
     * 日期: 2018/9/6
     * 说明: 附件文件归档
     */
    @ResponseBody
    @RequestMapping("/saveAttach")
    public BaseWrappers saveAttach(HttpServletRequest request, HttpServletResponse response , String attachmentId, String attachmentName, String htmlContent, Integer runId) {
        return budgetingProcessService.saveAttach(request,response,attachmentId,attachmentName,htmlContent,runId);
    }

    @ResponseBody
    @RequestMapping("/selBudgetingProcessByItem")
    public BaseWrappers selBudgetingProcessByItem(Integer budgetId){
        return budgetingProcessService.selBudgetingProcessByItem(budgetId);
    }


    @RequestMapping(value = "/workUpload", produces = {"application/json;charset=UTF-8"},method = RequestMethod.POST)
    @ResponseBody
    public void workUpload(@RequestParam(value = "file", required = false) MultipartFile[] files , String module, String runId,
                           HttpServletRequest request, HttpServletResponse response, String flag) throws IOException {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
        String company="xoa"+(String) request.getSession().getAttribute(
                "loginDateSouse");
        String flowPrcs = request.getParameter("flowPrcs");
        if (StringUtils.checkNull((String) request.getSession().getAttribute(
                "loginDateSouse"))){
            company = "xoa1001";
        } OutputStreamWriter ow = null;
try{
        ToJson<Attachment> toJson = budgetingProcessService.workUpload(files, company, module ,runId,flowPrcs,request,flag);
        response.setHeader("content-type", "text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream out = response.getOutputStream();
         ow = new OutputStreamWriter(out,"UTF-8"); ow.write(JSONObject.toJSONString(toJson));
}catch(IOException e){e.printStackTrace();}finally{ ow.flush();
    ow.close();}

    }

}
